package org.example;

public class Test1 {
    /**
     * 将数组分为小于和大于等于两部分，求mid就使用mid=(left + right) / 2
     * @param nums
     * @param target
     * @return
     */
    public int search(int[] nums, int target) {
        int n = nums.length;
        int left = 0, right = n - 1, mid = 0;
        while (left < right) {
            mid = (left + right) / 2;
            if (nums[mid] < target) {
                left = mid + 1;
            }else {
                right = mid;
            }
        }
        return nums[mid] == target ? mid : -1;
    }

    /**
     * 将数组分为小于等于和大于两部分，则求mid使用mid=(left + right + 1) / 2，否则就会陷入死循环
     * @param nums
     * @param target
     * @return
     */
    public int search1(int[] nums, int target) {
        int n = nums.length;
        int left = 0, right = n - 1, mid = 0;
        while (left < right) {
            mid = (left + right) / 2;
            if (nums[mid] < target) {
                left = mid + 1;
            }else {
                right = mid;
            }
        }
        return nums[mid] == target ? mid : -1;
    }

    public static void main(String[] args) {
        Test1 test1 = new Test1();
        int[] arr = {-1,0,3,5,9,12};
        test1.search(arr,9);
        System.out.println(666);
    }
}
